#include "configfilereader.h"

// trim from start
static inline std::string &ltrim(std::string &s) {
        s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
        return s;
}

// trim from end
static inline std::string &rtrim(std::string &s) {
        s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
        return s;
}

// trim from both ends
static inline std::string &trim(std::string &s) {
        return ltrim(rtrim(s));
}

configfilereader::configfilereader(string filename)
{
    fileName=filename;

    firewallIP="";
    firewallAddressList="";
    firewallUser="";
    firewallPass="";
    pidFile="";
    leasesFile="";
    listenPort=-1;
    scannerThreshold=-1;
    attackLeaseTimeout=-1;
    logLevel=-1;

    readConfigFile(fileName);
}

void configfilereader::readConfigFile(string fileName)
{

    string line;
    ifstream myfile (fileName.c_str());
    if (myfile.is_open())
    {
        while ( myfile.good() )
        {
            getline (myfile,line);
            size_t found;
            found=line.find("=");
            if ((int)found>0)
            {
                string option=line.substr(0,found);
                trim(option);
                string value=line.substr(found+1);
                trim(value);

                if (option.compare("firewallIP")==0)
                {
                    firewallIP=value;
                }

                if (option.compare("firewallAddressList")==0)
                {
                    firewallAddressList=value;
                }

                if (option.compare("firewallUser")==0)
                {
                    firewallUser=value;
                }

                if (option.compare("firewallPass")==0)
                {
                    firewallPass=value;
                }

                if (option.compare("listenPort")==0)
                {
                    listenPort=atoi(value.c_str());
                }

                if (option.compare("scannerThreshold")==0)
                {
                    scannerThreshold=atoi(value.c_str());
                }

                if (option.compare("attackLeaseTimeout")==0)
                {
                    attackLeaseTimeout=atoi(value.c_str());
                }

                if (option.compare("logLevel")==0)
                {
                    logLevel=atoi(value.c_str());
                }

                if (option.compare("pidFile")==0)
                {
                    pidFile=value;
                }

                if (option.compare("leasesFile")==0)
                {
                    leasesFile=value;
                }

            }
        }
        myfile.close();
    }
}

string configfilereader::checkOptions()
{

    if (firewallIP.compare("")==0)
        return "No firewall IP";
    if (firewallAddressList.compare("")==0)
        return "No firewall AddressList";
    if (firewallUser.compare("")==0)
        return "No firewall User";
    if (firewallPass.compare("")==0)
        return "No firewall Pass";
    if (listenPort==-1)
        return "No listenPort";
    if (scannerThreshold==-1)
        return "No scannerThreshold";
    if (attackLeaseTimeout==-1)
        return "No attackLeaseTimeout";
    if (logLevel==-1)
        return "No logLevel";
    if (pidFile.compare("")==0)
        return "No Pid File";
    if (leasesFile.compare("")==0)
        return "No Leases File";

    return "OK";

}

string configfilereader::getConfigFilename()
{
    return fileName;
}

string configfilereader::getFirewallIP()
{
    return firewallIP;
}

string configfilereader::getFirewallAddressList()
{
    return firewallAddressList;
}

string configfilereader::getFirewallUser()
{
    return firewallUser;
}

string configfilereader::getFirewallPass()
{
    return firewallPass;
}

int configfilereader::getListenPort()
{
    return listenPort;
}

int configfilereader::getScannerThreshold()
{
    return scannerThreshold;
}

int configfilereader::getAttackLeaseTimeout()
{
    return attackLeaseTimeout;
}

int configfilereader::getLogLevel()
{
    return logLevel;
}

string configfilereader::getPidFile()
{
    return pidFile;
}

string configfilereader::getLeasesFile()
{
    return leasesFile;
}
